<?php 
	
/***********************************************strat用户API***************************************************/ 
	/**
	 * taobao.user.get:得到单个用户信息 [查]
	 * 用户登陆才能获取隐私信息(不支持:location.address,real_name,id_card,phone,mobile,email ) 
	 **/
	 function TOP_UserGet($userParamArr){
	    $client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,userGet); 
	 }
	/**
	 * taobao.users.get :获取多个用户信息    [查]
	 * 查询字段：User数据结构的公开信息字段列表，以半角逗号(,)分隔
	 * 用户昵称，多个以半角逗号(,)分隔,最多40个 
	 **/
	 function TOP_UsersGet($userParamArr){
	 	$client = new Client();  
	    setUserParameter($client,$userParamArr);  
	    return returnCallBackPage($client,usersGet);  
	 }  
/*************************************************end用户API***************************************************/  
	 /**
	  * 买家转OS用户
	  * */
	 function  BuyerToUser( $nick ){
	 	   global  $db,$os; 
	 	   if($nick  && !Check_is_exist('tb_user','nick',$nick) ) GetInsertUserById($nick); 
	 	   $sql="select CONCAT('tb_',tb_u.nick) as user_name, (case tb_u.sex when 'm' then 1 when 'f' then 0 else 2 end) as sex ,real_name AS nick_name, 
	 	   		phone as home_phone,mobile as mobile_phone,tb_u.email as email,alipay_no as alias, 
				outer_user_id,zip,address,city,state,
				country,district from open_tb_user as tb_u 
				left join open_tb_location as tb_l on tb_u.nick=tb_l.nick 
				where tb_u.nick ='".$nick."'";   
	 	   if( $user=$db->getRow($sql)){ //已经存在taobao用户表中
	 	   	   $users =& init_users(); 
               $users->add_user($user['user_name'], md5('888888'), $user['email']) ; //尝试注册
	 	   	   $user['reg_time'] = gmtime();  
 	 	   	   OS_InsertIntoTB('users','user_name',$user);//把taobao用户转换为OS商城用户   
			   $os_user_id =Check_is_exist('users','user_name',$user['user_name'],'','user_id');
			   Update_table_key('tb_user','outer_user_id',$os_user_id,"nick='".$nick."'"); //回写open_tb_user  
			   $address = array(  						   /* 更新客户地址 */
								'country'    =>  0,
								'province'   => Check_is_exist('region','region_type', 1  ,"region_name like '%".str_replace_region( $user['state'] )."%' ",'region_id' ),
								'city'       => Check_is_exist('region','region_name',str_replace_region( $user['city'] ),"region_type=2",'region_id' ),
								'district'   => Check_is_exist('region','region_name',str_replace_region( $user['district'] ),"region_type=3",'region_id' ),
								'address_name'  => $user['address'],
								'address'    => $user['address'],
								'consignee'  => $user['nick_name'],
								'email'      => $user['email'],
								'tel'        => $user['home_phone'],
								'mobile'     => $user['mobile_phone'],
								'best_time'  =>  '',
								'sign_building' =>  '',
								'zipcode'       =>$user['zip'],
								'user_id'       =>$os_user_id,
					       ); 
			    OS_InsertIntoTB('user_address','user_id',$address,"address='".addslashes($user['address'])."'");
	 	    } 
	 	    return $os_user_id;
	 }
	 /**
 	   * taobao.user.get:得到单个用户信息  [查]
	   * 得到用户ID 如果不存在则插入此用户
	   */ 
	 function GetInsertUserById($nick){
	  	$fields  = 'user_id,nick,sex,buyer_credit,seller_credit,location, 
					created ,last_visit ,birthday , type ,has_more_pic ,item_img_num ,item_img_size ,prop_img_num ,
					prop_img_size ,auto_repost ,promoted_type , status ,alipay_bind ,consumer_protection ';
		$nick   = $nick; 
		$User =  TOP_UserGet(  array('fields'=>$fields, 'nick'=>$nick)  );  
		if(!$User['user']) return;
		OS_InsertIntoTB('tb_user','nick', $User['user'] ); 
		$User_id= $User['user']['user_id'] ;
		if($User['user']['buyer_credit']) {  //买家信用 
			$User['user']['buyer_credit']['user_id']= $User_id ;
			$User['user']['buyer_credit']['nick']= $nick ;
			$User['user']['buyer_credit']['credit_type']= 'buyer_credit' ;   
		    OS_InsertIntoTB('tb_usercredit','user_id', $User['user']['buyer_credit'],"credit_type='buyer_credit'" );  //信用表
			if($buyer_credit_id=Check_is_exist('tb_usercredit','user_id',$User_id,"credit_type='buyer_credit'") )  Update_table_key('tb_user','buyer_credit',$buyer_credit_id, "user_id='".$User_id."'" ) ; //回写用户表
		 } 
		if($User['user']['seller_credit']) { //卖家信用 
			$User['user']['seller_credit']['user_id']= $User_id ;
			$User['user']['seller_credit']['nick']= $nick ;
			$User['user']['seller_credit']['credit_type']= 'seller_credit' ;   
			$seller_credit_id = OS_InsertIntoTB('tb_usercredit','user_id', $User['user']['seller_credit'],"credit_type='seller_credit'" ); //买家信用表
		 	if($seller_credit_id=Check_is_exist('tb_usercredit','user_id',$User_id,"credit_type='seller_credit'") )  Update_table_key('tb_user','seller_credit',$seller_credit_id, "user_id='".$User_id."'") ;  //回写用户表
		} 
 		if($User['user']['location'] ){  //用户当前居住地公开信息 
 			$User['user']['location']['user_id']= $User_id ;
 			$User['user']['location']['nick']= $nick ; 
 			OS_InsertIntoTB('tb_location','user_id', $User['user']['location'] );  
 		    if($location_id=Check_is_exist('tb_location','nick',$nick) )  Update_table_key('tb_user','location',$location_id, "user_id='".$User_id."'" ) ;  //回写用户表
 		}   
	  } 
	  /**
 	   * taobao.user.get:得到单个用户信息  [查]
	   * 得到用户ID 如果不存在则插入此用户
	   */
     function Get_user_id($result)
    {
    	global $exc,$db,$os,$topParamArr;  
		$fields  = 'user_id,nick,sex,buyer_credit,seller_credit,location, 
					created ,last_visit ,birthday , type ,has_more_pic ,item_img_num ,item_img_size ,prop_img_num ,
					prop_img_size ,auto_repost ,promoted_type , status ,alipay_bind ,consumer_protection ';
		$nick   =  $result['buyer_nick']; 
		$User =  TOP_UserGet(  array('fields'=>$fields, 'nick'=>$nick)  );   
		
		$user_nick = "tb_".$result['buyer_nick'] ;  //taobao网的nike 为 tb_购物商账  
		$sql = "select user_id from ".$os->table('users')." where user_name ='".$user_nick."'";   
		if ( $user_id = $db->getOne($sql) ){ 
			return $user_id;  //返回用户ID 
		}  
		else { 
		    return InsertIntoUser($result); //插入新用户并返回用户ID
		}  
    }
  /**
   *  添加客户表 
   */
    function  InsertIntoUser($result)
     {
     	print_r($result);
     	die();
         global $exc,$db,$os,$topParamArr;  
		 $user_nick = "tb_".$result['buyer_nick'] ;  //taobao网的nike 为 tb_购物商账  
		 $user = array(  'rank_points'   => $_CFG['register_points'],
                'pay_points'    => $_CFG['register_points'],
                'sex'           => 0,
                'credit_line'   => 0
                );
	    $username = $user_nick;
	    $nickname =  $user_nick;
	    $password = md5('000000');
	    $email = '';
	    $sex = 2 ; 
	    $birthday = '';
	    $rank = 0;
	    $credit_line = 0; 
	    $users =& init_users();   
        $email=$username;
    if (!$users->add_user($username, $password, $email,'111','222','333'  )  ) {/* 插入客户数据失败 */ 
        if ($users->error == ERR_INVALID_USERNAME){ 
            $msg = '无效的客户名';
        } elseif ($users->error == ERR_USERNAME_NOT_ALLOW) { 
            $msg = '客户名不允许注册';
        } elseif ($users->error == ERR_USERNAME_EXISTS)  {  
            $msg = '已经存在一个相同的客户名。';
        }  elseif ($users->error == ERR_INVALID_EMAIL) { 
            $msg = '无效的email地址';
        }elseif ($users->error == ERR_EMAIL_NOT_ALLOW){ 
            $msg = '邮件不允许';
        }elseif ($users->error == ERR_EMAIL_EXISTS){ 
            $msg = '该邮件地址已经存在。';
        }else {  
            //die('Error:'.$users->error_msg());
        }
        sys_msg($msg, 1);
    } 
    /* 注册送积分 */
    if (!empty($GLOBALS['_CFG']['register_points'])) { 
        log_account_change($_SESSION['user_id'], 0, 0, $GLOBALS['_CFG']['register_points'], $GLOBALS['_CFG']['register_points'], '注册送积分');
    } 
    /* 更新客户的其它信息 */
    $other =  array();
    $other['credit_line'] = $credit_line;
    $other['user_rank']  = $rank;
    $other['sex']        = $sex;
    $other['birthday']   = $birthday;
    $other['nick_name']  = $nickname;
    $other['reg_time']   = gmtime(); 
    $db->autoExecute($os->table('users'), $other, 'UPDATE', "user_name = '$username'"); 
    $sql_id = "SELECT user_id FROM ".$os->table('users')." WHERE user_name = '".$username."'"; 
    return   $user_id = $db->getOne($sql_id); 
     }
?>